<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use think\Db;

class Index extends Frontend {

    public function index() {
//        var_dump($this->request);die;
        $cate_id = $this->request->param('cate_id', 0);


        //TODO 获取当前小区内的商品
        //TODO 获取当前小区内商品的类型
        $cate_list = db('goods_cate')->field('cate_id,cate_title')->where(['status' => 1, 'is_deleted' => 0])->order('sort')->select();

        $gwhere['g.is_deleted'] = 0;
        $gwhere['g.status'] = 1;
        $gwhere['ubg.stock_num'] = ['gt', 0];
        $gwhere['ubg.is_del'] = 0;

        if ($cate_id) {
            $gwhere['g.cate_id'] = $cate_id;
        }

        $gwhere['ubg.user_box_id'] = $this->userBoxId;

        $goods_list = db('user_box_goods')
                ->alias('ubg')
                ->field('ubg.box_goods_id,g.goods_id,gb.brand_title,g.goods_title,g.goods_model,g.goods_spec,g.goods_desc,g.goods_logo,g.market_price,ubg.selling_price')
                ->join('fa_goods g', 'g.goods_id = ubg.goods_id')
                ->join('fa_goods_brand gb', 'g.brand_id = gb.brand_id')
                ->where($gwhere)
                ->limit(6)
                ->order('g.createtime')
                ->select();
        if ($goods_list) {
            //获取用户购物车数量
            foreach ($goods_list as &$val) {
                $val['buy_num'] = getCardNumByGoods($this->iUserId, $val['box_goods_id']);
            }
        }


        //购物车数量及价格
        $cart = getCartTotalByUserId($this->iUserId);


        //获取banner列表
        $banner_list = db('ad')
                ->where(['postion_id' => 1, 'type' => 1])
                ->select();

        //文字广告
        $banner_text_list = db('ad')
                ->where(['postion_id' => 1, 'type' => 2])
                ->select();

        
        $this->assign('cart', $cart);
        $this->assign('goods_list', $goods_list);
        $this->assign('cate_id', $cate_id);
        $this->assign('cate_list', $cate_list);
        $this->assign('banner_list', $banner_list);
        $this->assign('banner_text_list', $banner_text_list);
        return $this->view->fetch();
    }

    public function cart() {

        $list = db('cart')
                ->alias('c')
                ->field('ubg.box_goods_id,g.goods_id,gb.brand_title,g.goods_title,g.goods_model,g.goods_desc,g.goods_logo,g.goods_spec,g.market_price,ubg.selling_price')
                ->join('fa_user_box_goods ubg', 'ubg.box_goods_id = c.box_goods_id')
                ->join('fa_goods g', 'ubg.goods_id=g.goods_id')
                ->join('fa_goods_brand gb', 'g.brand_id = gb.brand_id')
                ->where(['c.user_id' => $this->iUserId, 'c.type' => 0, 'ubg.stock_num' => ['gt', 0], 'ubg.is_del' => 0])
                ->order('c.id desc')
                ->select();


        if ($list) {
            foreach ($list as &$val) {
                $val['buy_num'] = getCardNumByGoods($this->iUserId, $val['box_goods_id']);
            }
        }
        //购物车数量及价格
        $cart = getCartTotalByUserId($this->iUserId);

        $this->assign('list', $list);
        $this->assign('cart', $cart);
        return $this->view->fetch();
    }

    public function item() {
        $cate_id = $this->request->param('cate_id', 0);
        $p = $this->request->param('p', 0);
        $size = $this->request->param('psize', 6);
        $page = 0;
        if ($p) {
            $page = $p * $size;
        }

        $gwhere['g.is_deleted'] = 0;
        $gwhere['g.status'] = 1;
        $gwhere['ubg.stock_num'] = ['gt', 0];
        $gwhere['ubg.is_del'] = 0;
        if ($cate_id) {
            $gwhere['g.cate_id'] = $cate_id;
        }

        $gwhere['ubg.user_box_id'] = $this->userBoxId;

        $goods_list = db('user_box_goods')
                ->alias('ubg')
                ->field('ubg.box_goods_id,g.goods_id,gb.brand_title,g.goods_title,g.goods_model,g.goods_spec,g.goods_desc,g.goods_logo,g.market_price,ubg.selling_price')
                ->join('fa_goods g', 'g.goods_id = ubg.goods_id')
                ->join('fa_goods_brand gb', 'g.brand_id = gb.brand_id')
                ->where($gwhere)
                ->limit("$page,$size")
                ->order('g.createtime')
                ->select();

        if ($goods_list) {
            //获取用户购物车数量
            foreach ($goods_list as &$val) {
                $val['buy_num'] = getCardNumByGoods($this->iUserId, $val['box_goods_id']);
            }
        }
        $this->assign('goods_list', $goods_list);
        return $this->view->fetch();
    }

}
